﻿using System;
using System.Linq;
using System.Text;

namespace HslCommunication.Profinet.Keyence
{
	/// <summary>
	/// 当前SR2000的辅助函数
	/// </summary>
	// Token: 0x02000090 RID: 144
	internal class KeyenceSR2000Helper
	{
		/// <summary>
		/// 读取条码信息，返回最终读取的条码数据<br />
		/// Read the barcode information and return the finally read barcode data
		/// </summary>
		/// <param name="readCore">核心交互的方法</param>
		/// <returns>条码信息</returns>
		// Token: 0x06000AF7 RID: 2807 RVA: 0x00041C40 File Offset: 0x0003FE40
		public static OperateResult<string> ReadBarcode(Func<byte[], OperateResult<byte[]>> readCore)
		{
			OperateResult<string> operateResult = KeyenceSR2000Helper.ReadCustomer("LON", readCore);
			bool flag = !operateResult.IsSuccess;
			if (flag)
			{
				bool flag2 = operateResult.ErrorCode < 0;
				if (flag2)
				{
					KeyenceSR2000Helper.ReadCustomer("LOFF", readCore);
				}
			}
			return operateResult;
		}

		/// <summary>
		/// 复位命令，响应后，进行复位动作。<br />
		/// Reset command, after responding, reset action.
		/// </summary>
		/// <param name="readCore">核心交互的方法</param>
		/// <returns>返回是否成功的结果对象</returns>
		// Token: 0x06000AF8 RID: 2808 RVA: 0x00041C87 File Offset: 0x0003FE87
		public static OperateResult Reset(Func<byte[], OperateResult<byte[]>> readCore)
		{
			return KeyenceSR2000Helper.ReadCustomer("RESET", readCore);
		}

		/// <summary>
		/// 打开指示灯<br />
		/// Turn on the indicator
		/// </summary>
		/// <param name="readCore">核心交互的方法</param>
		/// <returns>返回是否成功的结果对象</returns>
		// Token: 0x06000AF9 RID: 2809 RVA: 0x00041C94 File Offset: 0x0003FE94
		public static OperateResult OpenIndicator(Func<byte[], OperateResult<byte[]>> readCore)
		{
			return KeyenceSR2000Helper.ReadCustomer("AMON", readCore);
		}

		/// <summary>
		/// 关闭指示灯<br />
		/// Turn off the indicator
		/// </summary>
		/// <param name="readCore">核心交互的方法</param>
		/// <returns>返回是否成功的结果对象</returns>
		// Token: 0x06000AFA RID: 2810 RVA: 0x00041CA1 File Offset: 0x0003FEA1
		public static OperateResult CloseIndicator(Func<byte[], OperateResult<byte[]>> readCore)
		{
			return KeyenceSR2000Helper.ReadCustomer("AMOFF", readCore);
		}

		/// <summary>
		/// 读取基恩士的版本<br />
		/// Read Keyence's version
		/// </summary>
		/// <param name="readCore">核心交互的方法</param>
		/// <returns>版本信息</returns>
		// Token: 0x06000AFB RID: 2811 RVA: 0x00041CAE File Offset: 0x0003FEAE
		public static OperateResult<string> ReadVersion(Func<byte[], OperateResult<byte[]>> readCore)
		{
			return KeyenceSR2000Helper.ReadCustomer("KEYENCE", readCore);
		}

		/// <summary>
		/// 读取基恩士的命令状态，none:不处理；wait：等待设置反映；update：正在更新<br />
		/// Read the command status of Keyence, none: do not process; wait: wait for the setting to reflect; update: update
		/// </summary>
		/// <param name="readCore">核心交互的方法</param>
		/// <returns>返回是否成功的结果对象</returns>
		// Token: 0x06000AFC RID: 2812 RVA: 0x00041CBB File Offset: 0x0003FEBB
		public static OperateResult<string> ReadCommandState(Func<byte[], OperateResult<byte[]>> readCore)
		{
			return KeyenceSR2000Helper.ReadCustomer("CMDSTAT", readCore);
		}

		/// <summary>
		/// 读取基恩士的错误状态<br />
		/// Read the error status of Keyence
		/// </summary>
		/// <param name="readCore">核心交互的方法</param>
		/// <returns>返回是否成功的结果对象</returns>
		// Token: 0x06000AFD RID: 2813 RVA: 0x00041CC8 File Offset: 0x0003FEC8
		public static OperateResult<string> ReadErrorState(Func<byte[], OperateResult<byte[]>> readCore)
		{
			return KeyenceSR2000Helper.ReadCustomer("ERRSTAT", readCore);
		}

		/// <summary>
		/// 读取IN端子的状况，需要传入哪个IN端子，返回是否通断<br />
		/// Read the status of the IN terminal, which IN terminal needs to be passed in, and return whether it is on or off
		/// </summary>
		/// <param name="number">端子的信息</param>
		/// <param name="readCore">核心交互的方法</param>
		/// <returns>返回是否成功的结果对象</returns>
		// Token: 0x06000AFE RID: 2814 RVA: 0x00041CD8 File Offset: 0x0003FED8
		public static OperateResult<bool> CheckInput(int number, Func<byte[], OperateResult<byte[]>> readCore)
		{
			OperateResult<string> operateResult = KeyenceSR2000Helper.ReadCustomer("INCHK," + number.ToString(), readCore);
			bool flag = !operateResult.IsSuccess;
			OperateResult<bool> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool>(operateResult);
			}
			else
			{
				bool flag2 = operateResult.Content == "ON";
				if (flag2)
				{
					result = OperateResult.CreateSuccessResult<bool>(true);
				}
				else
				{
					bool flag3 = operateResult.Content == "OFF";
					if (flag3)
					{
						result = OperateResult.CreateSuccessResult<bool>(false);
					}
					else
					{
						result = new OperateResult<bool>(operateResult.Content);
					}
				}
			}
			return result;
		}

		/// <summary>
		/// 设置OUT端子的使能，需要传入哪个OUT端子，返回是否设置成功！<br />
		/// Set the enable of the OUT terminal, which OUT terminal needs to be passed in, and return whether the setting is successful!
		/// </summary>
		/// <param name="number">端子的索引，1，2，3</param>
		/// <param name="value">是否通断</param>
		/// <param name="readCore">核心交互的方法</param>
		/// <returns>返回是否成功的结果对象</returns>
		// Token: 0x06000AFF RID: 2815 RVA: 0x00041D5F File Offset: 0x0003FF5F
		public static OperateResult SetOutput(int number, bool value, Func<byte[], OperateResult<byte[]>> readCore)
		{
			return KeyenceSR2000Helper.ReadCustomer((value ? "OUTON," : "OUTOFF,") + number.ToString(), readCore);
		}

		/// <summary>
		/// 读取扫码器的扫码记录，返回数组数据，分别是成功次数，失败次数，ERROR次数，稳定次数，时机输入次数<br />
		/// Read the scan code record of the scanner and return the array data, which are the number of successes, failures, ERRORs, stable times, and timing input times.
		/// </summary>
		/// <param name="readCore">核心交互的方法</param>
		/// <returns>返回是否成功的结果对象</returns>
		// Token: 0x06000B00 RID: 2816 RVA: 0x00041D84 File Offset: 0x0003FF84
		public static OperateResult<int[]> ReadRecord(Func<byte[], OperateResult<byte[]>> readCore)
		{
			OperateResult<string> operateResult = KeyenceSR2000Helper.ReadCustomer("NUM", readCore);
			bool flag = !operateResult.IsSuccess;
			OperateResult<int[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<int[]>(operateResult);
			}
			else
			{
				result = OperateResult.CreateSuccessResult<int[]>(Enumerable.ToArray<int>(Enumerable.Select<string, int>(operateResult.Content.Split(new char[]
				{
					','
				}), (string n) => int.Parse(n))));
			}
			return result;
		}

		/// <summary>
		/// 锁定扫码设备<br />
		/// Lock scanning device
		/// </summary>
		/// <param name="readCore">核心交互的方法</param>
		/// <returns>返回是否成功的结果对象</returns>
		// Token: 0x06000B01 RID: 2817 RVA: 0x00041DFC File Offset: 0x0003FFFC
		public static OperateResult Lock(Func<byte[], OperateResult<byte[]>> readCore)
		{
			return KeyenceSR2000Helper.ReadCustomer("LOCK", readCore);
		}

		/// <summary>
		/// 解除锁定的扫码设备<br />
		/// Unlock Scanning Device
		/// </summary>
		/// <param name="readCore">核心交互的方法</param>
		/// <returns>返回是否成功的结果对象</returns>
		// Token: 0x06000B02 RID: 2818 RVA: 0x00041E09 File Offset: 0x00040009
		public static OperateResult UnLock(Func<byte[], OperateResult<byte[]>> readCore)
		{
			return KeyenceSR2000Helper.ReadCustomer("UNLOCK", readCore);
		}

		/// <summary>
		/// 读取自定义的命令，例如LON，如果是包含其他参数的，比如打开OUT端子，OUTON,1<br />
		/// Read custom commands, such as LON, if it contains other parameters, such as open OUT terminal, OUTON,1
		/// </summary>
		/// <param name="command">自定义的命令</param>
		/// <param name="readCore">核心的数据交互方法</param>
		/// <returns>返回是否成功的结果对象</returns>
		// Token: 0x06000B03 RID: 2819 RVA: 0x00041E18 File Offset: 0x00040018
		public static OperateResult<string> ReadCustomer(string command, Func<byte[], OperateResult<byte[]>> readCore)
		{
			byte[] bytes = Encoding.ASCII.GetBytes(command + "\r");
			string text = command;
			bool flag = command.IndexOf(',') > 0;
			if (flag)
			{
				text = command.Substring(0, command.IndexOf(','));
			}
			OperateResult<byte[]> operateResult = readCore(bytes);
			bool flag2 = !operateResult.IsSuccess;
			OperateResult<string> result;
			if (flag2)
			{
				result = operateResult.Convert<string>(null);
			}
			else
			{
				string text2 = Encoding.ASCII.GetString(operateResult.Content).Trim(new char[]
				{
					'\r'
				});
				bool flag3 = text2.StartsWith("ER," + text + ",");
				if (flag3)
				{
					result = new OperateResult<string>(KeyenceSR2000Helper.GetErrorDescription(text2.Substring(4 + text.Length)));
				}
				else
				{
					bool flag4 = text2.StartsWith("OK," + text);
					if (flag4)
					{
						bool flag5 = text2.Length > 4 + text.Length;
						if (flag5)
						{
							return OperateResult.CreateSuccessResult<string>(text2.Substring(4 + text.Length));
						}
					}
					result = OperateResult.CreateSuccessResult<string>(text2);
				}
			}
			return result;
		}

		/// <summary>
		/// 获取操作代码包含的错误文本信息<br />
		/// Get the error text information contained in the operation code
		/// </summary>
		/// <param name="error">错误代码</param>
		/// <returns>真是的错误信息</returns>
		// Token: 0x06000B04 RID: 2820 RVA: 0x00041F30 File Offset: 0x00040130
		public static string GetErrorDescription(string error)
		{
			uint num = <PrivateImplementationDetails>.ComputeStringHash(error);
			if (num <= 518729469U)
			{
				if (num <= 485174231U)
				{
					if (num <= 401286136U)
					{
						if (num != 352233207U)
						{
							if (num == 401286136U)
							{
								if (error == "14")
								{
									return StringResources.Language.KeyenceSR2000Error14;
								}
							}
						}
						else if (error == "99")
						{
							return StringResources.Language.KeyenceSR2000Error99;
						}
					}
					else if (num != 468396612U)
					{
						if (num == 485174231U)
						{
							if (error == "11")
							{
								return StringResources.Language.KeyenceSR2000Error11;
							}
						}
					}
					else if (error == "10")
					{
						return StringResources.Language.KeyenceSR2000Error10;
					}
				}
				else if (num <= 501951850U)
				{
					if (num != 485321326U)
					{
						if (num == 501951850U)
						{
							if (error == "12")
							{
								return StringResources.Language.KeyenceSR2000Error12;
							}
						}
					}
					else if (error == "05")
					{
						return StringResources.Language.KeyenceSR2000Error05;
					}
				}
				else if (num != 502098945U)
				{
					if (num == 518729469U)
					{
						if (error == "13")
						{
							return StringResources.Language.KeyenceSR2000Error13;
						}
					}
				}
				else if (error == "04")
				{
					return StringResources.Language.KeyenceSR2000Error04;
				}
			}
			else if (num <= 569209421U)
			{
				if (num <= 535654183U)
				{
					if (num != 518876564U)
					{
						if (num == 535654183U)
						{
							if (error == "02")
							{
								return StringResources.Language.KeyenceSR2000Error02;
							}
						}
					}
					else if (error == "03")
					{
						return StringResources.Language.KeyenceSR2000Error03;
					}
				}
				else if (num != 552431802U)
				{
					if (num == 569209421U)
					{
						if (error == "00")
						{
							return StringResources.Language.KeyenceSR2000Error00;
						}
					}
				}
				else if (error == "01")
				{
					return StringResources.Language.KeyenceSR2000Error01;
				}
			}
			else if (num <= 2381486463U)
			{
				if (num != 2364708844U)
				{
					if (num == 2381486463U)
					{
						if (error == "20")
						{
							return StringResources.Language.KeyenceSR2000Error20;
						}
					}
				}
				else if (error == "21")
				{
					return StringResources.Language.KeyenceSR2000Error21;
				}
			}
			else if (num != 2398264082U)
			{
				if (num == 2415041701U)
				{
					if (error == "22")
					{
						return StringResources.Language.KeyenceSR2000Error22;
					}
				}
			}
			else if (error == "23")
			{
				return StringResources.Language.KeyenceSR2000Error23;
			}
			return StringResources.Language.UnknownError + " :" + error;
		}
	}
}
